//
//  UnCompleteViewController.m
//  lhpc_manager
//
//  Created by 李英杰 on 2017/9/6.
//  Copyright © 2017年 zhaoyongjie. All rights reserved.
//

#import "UnCompleteViewController.h"
#import "OrderTableViewCell.h"
#import "WithDrawViewController.h"
#import "ResetPasswordViewController.h"
#import "LHNetworking.h"
#import "OrderModel.h"
#import <YYKit/YYKit.h>
#import <CYLTableViewPlaceHolder/CYLTableViewPlaceHolder.h>
#import "LHEmptyView.h"

@interface UnCompleteViewController ()<UITableViewDelegate,UITableViewDataSource,CYLTableViewPlaceHolderDelegate>

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, strong) UIView * withDrawView;
@property (nonatomic, strong) UIButton * withDrawButton;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) NSMutableArray * dataArray;

/**
 是否设置过支付密码
 */
@property (nonatomic, assign) BOOL hasSetPsw;

@end

@implementation UnCompleteViewController

- (UIView *)makePlaceHolderView {
    return [[LHEmptyView alloc] init];
}

- (BOOL)enableScrollWhenPlaceHolderViewShowing {
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = BACKCOLOR;
    
    [HLNoteCenter addObserver:self selector:@selector(getData) name:LHReloadUnCompleteHome object:nil];
    
    [self setMainView];
    
    [self initData];
    
    [self getData];
}

- (void)initData {
    self.page = 0;
}

-(void)getData{
    
    NSDictionary * parameters = @{@"page" : @(self.page), @"size" : @(10)};
    
    LHPC_WEAK_SELF;
    [[LHNetworking shared] POST:LHSetRequestAPI(LHHomeUnWithDrawList) parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        LHPC_STRONG_SELF;
        
        NSLog(@"%@",responseObject);
        if (responseObject) {
            [strongSelf handleResultWithDic:responseObject];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        LHPC_STRONG_SELF;
        [strongSelf.tableView.mj_header endRefreshing];
        [strongSelf.tableView.mj_footer endRefreshing];
        [HLProgressHUD showHudTipStr:LHNetWorkErrorMessage];
    }];
}

- (void)handleResultWithDic:(NSDictionary  *)response {
    NSArray *arr = response[@"data"][@"stroke"];
    self.hasSetPsw = [response[@"data"][@"password"] boolValue];
    self.tableView.mj_footer.hidden = !(arr.count > 0);
    if (self.page == 0) {
        [self.dataArray removeAllObjects];
    }
    
    if (self.page == 0) {
        [self.tableView.mj_header endRefreshing];
        if (arr.count < 10) {
            self.tableView.mj_footer.hidden = YES;
        }else{
            [self.tableView.mj_footer resetNoMoreData];
        }
        
    } else if (arr.count < 10) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } else {
        [self.tableView.mj_footer endRefreshing];
    }
    
    for (int i = 0; i < arr.count; i++) {
        OrderModel *model = [OrderModel modelWithJSON:arr[i]];
        [self.dataArray addObject:model];
    }
    
    [self.tableView cyl_reloadData];
}

-(void)comfirmWithDraw{
    
    [HLProgressHUD showHUDWithStyle:HLProgressHUDStyleLoading status:@""];
    LHPC_WEAK_SELF;
    [[LHNetworking shared] POST:LHSetRequestAPI(LHGetWithDrawMoney) parameters:@{} success:^(NSURLSessionDataTask *task, id responseObject) {
        LHPC_STRONG_SELF;
        if (responseObject) {
            NSDictionary * resultDic = responseObject[@"data"];
            double cash = [resultDic[@"cash"] doubleValue];
            BOOL hasSet = [resultDic[@"password"] boolValue];
            if (cash == 0.0) {
                //可提现金额为0
                [HLProgressHUD showHUDWithStyle:HLProgressHUDStyleError status:@"当前无可提现金额"];
            }else if (hasSet) {
                // 提现界面
                WithDrawViewController *vc = [[WithDrawViewController alloc] init];
                vc.withDrawCount = cash;
                [strongSelf.navigationController pushViewController:vc animated:YES];
            }else{
                // 设置提现密码界面
                ResetPasswordViewController *vc = [[ResetPasswordViewController alloc] init];
                [strongSelf.navigationController pushViewController:vc animated:YES];
            }
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [HLProgressHUD showHudTipStr:LHNetWorkErrorMessage];
    }];

    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 140;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"uncompleteordertableviewcellid" forIndexPath:indexPath];
    
    
    [cell setOrderValueWith:self.dataArray[indexPath.row]];
    
    return cell;
    
}

-(void)setMainView{
    
    [self.view addSubview:self.withDrawView];
    [self.withDrawView addSubview:self.withDrawButton];
    
    [self.view addSubview:self.tableView];

}


-(void)updateViewConstraints{
    [super updateViewConstraints];
    
    [self.withDrawView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@60);
        
    }];
    
    [self.withDrawButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.withDrawView).offset(10);
        make.bottom.equalTo(self.withDrawView).offset(-10);
        make.left.equalTo(self.withDrawView).offset(20);
        make.right.equalTo(self.withDrawView).offset(-20);
        
    }];
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.withDrawView.mas_top);
        
    }];
}


-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.separatorColor = LINECOLOR;
        _tableView.backgroundColor = BACKCOLOR;
        
        
        [_tableView registerClass:[OrderTableViewCell class] forCellReuseIdentifier:@"uncompleteordertableviewcellid"];
        
        LHPC_WEAK_SELF;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            LHPC_STRONG_SELF;
            
            strongSelf.page = 0;
            strongSelf.tableView.mj_footer.state = MJRefreshStateIdle;
            [strongSelf getData];
            
        }];
        
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            LHPC_STRONG_SELF;
            weakSelf.page += 1;
            [strongSelf getData];
            
        }];
        
        _tableView.mj_footer.hidden = YES;
        
    }
    return _tableView;
}

-(UIView *)withDrawView{
    if (!_withDrawView) {
        _withDrawView = [[UIView alloc]init];
        _withDrawView.backgroundColor = [UIColor whiteColor];
        
        UILabel * line = [[UILabel alloc]init];
        [_withDrawView addSubview:line];
        line.backgroundColor = LINECOLOR;
        [line mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.left.right.equalTo(_withDrawView);
            make.height.equalTo(@0.5);
            
        }];
    }
    
    return _withDrawView;
}

-(UIButton *)withDrawButton{
    if (!_withDrawButton) {
        _withDrawButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _withDrawButton.layer.cornerRadius = 3;
        [_withDrawButton setBackgroundColor:ORANGCOLOR];
        [_withDrawButton setTitle:@"一键提现" forState:UIControlStateNormal];
        [_withDrawButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_withDrawButton addTarget:self action:@selector(comfirmWithDraw) forControlEvents:UIControlEventTouchUpInside];
        _withDrawButton.userInteractionEnabled = YES;
        
    }
    return _withDrawButton;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
